package com.logger.handlers;

import com.logger.LogMessage;
import com.logger.layouts.ALayout;


/**
 * 抽象的日志处理实现
 * Created by chenxunlin01 on 2016/1/16.
 */
public abstract class AHandler {

    /**
     * 处理器名称
     */
    private String name;

    /**
     * 是否要异步处理
     */
    private boolean async = false;

    /**
     * 布局
     */
    private ALayout layout;

    /**
     * 处理日志事件
     *
     * @param message
     */
    public synchronized void doHandler(LogMessage message) {
        String log = null;
        if (layout != null) {
            log = layout.format(message);
        }
        try {
            handler(message, log);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract void handler(LogMessage msg, String log) throws Exception;


    public boolean isAsync() {
        return async;
    }

    public void setAsync(boolean async) {
        this.async = async;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ALayout getLayout() {
        return layout;
    }

    public void setLayout(ALayout layout) {
        this.layout = layout;
    }
}
